package com.quanyan.financialSystem.service.place.impl;

import com.lefu.commons.utils.Page;
import com.quanyan.api.APIResponse;
import com.quanyan.common.Plugin.PageObj;
import com.quanyan.financialSystem.entity.vo.req.ReqPlaceInComeStream;
import com.quanyan.financialSystem.entity.vo.resp.*;
import com.quanyan.financialSystem.mapper.InComeStreamMapper;
import com.quanyan.financialSystem.service.place.PlaceInComeStreamService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/11/1.
 */
@Service("inComeStream")
public class InComeStreamImpl implements PlaceInComeStreamService{

    private static final Logger logger = LoggerFactory.getLogger(InComeStreamImpl.class);
    @Autowired
    private InComeStreamMapper inComeStreamMapper;

    @Override
    public APIResponse<PageObj<List<RespPlaceInComeStream>>> findAllPlaceInComeStream(ReqPlaceInComeStream reqPlaceInComeStream) {
        logger.info("开吃查询场地收入流水----》{}",reqPlaceInComeStream.toString());
        Page page = new Page();
        page.setCurrentPage(reqPlaceInComeStream.getPageNo());
        page.setShowCount(reqPlaceInComeStream.getPageSize());
        List<RespPlaceInComeStream> lstResult ;
        BigDecimal zero = new BigDecimal(0);// 0
        BigDecimal negativeOne = new BigDecimal(-1);//-1

        if(reqPlaceInComeStream.getPayType() == null || reqPlaceInComeStream.getPayType().intValue() == 99){//表示全部
            lstResult = inComeStreamMapper.findAllPlaceInComeStream(page,reqPlaceInComeStream);
        }else{
            lstResult = inComeStreamMapper.findAllPlaceInComeStreamByPayItem(page,reqPlaceInComeStream);
        }

        if(lstResult != null && lstResult.size() > 0){
            List<PayItem> lstPayItem = inComeStreamMapper.findPayItem(lstResult); // 支付方式
            List<BuyType> lstBuyType = inComeStreamMapper.findBuyType(lstResult);//购买方式
            List<RespPlaceInComeStream> lstMe= new ArrayList<>();//剔除会员消费
            List<RespReconciliation> lstRecon = inComeStreamMapper.findReconciliation(lstResult);//对账
            List<RespConsumeTime> lstConsume = inComeStreamMapper.findConsumeTime(lstResult);//对账

            List<RespPlaceInComeStream> lstMemberResult = null;//会员消费
            /****==============把会员消费的订单筛选出来===============***/
            for(PayItem p: lstPayItem){
                for (RespPlaceInComeStream respPlaceInComeStream : lstResult){
                    if(p.getPlaceOrderId().equals(respPlaceInComeStream.getPlaceOrderId()) && p.getPayType() == member_pay_type){
                        lstMe.add(respPlaceInComeStream);
                        break;
                    }
                }
            }
            if(lstMe.size() > 0)
               lstMemberResult = inComeStreamMapper.findMemberAmount(lstMe);

            for (RespPlaceInComeStream respPlaceInComeStream : lstResult){
                respPlaceInComeStream.setOriginalAmount(respPlaceInComeStream.getDebit());
                if(respPlaceInComeStream.getDebit() == null || respPlaceInComeStream.getDebit().compareTo(zero) == 0)
                    respPlaceInComeStream.setDebit(respPlaceInComeStream.getCredit());

                if(respPlaceInComeStream.getCredit() == null || respPlaceInComeStream.getCredit().compareTo(zero) == 0)
                    respPlaceInComeStream.setCredit(respPlaceInComeStream.getDebit());

                /**  =========== 退款 =========== **/
                if(respPlaceInComeStream.getOrderStatus().equals("5")){
                    respPlaceInComeStream.setDebit(respPlaceInComeStream.getCredit().multiply(negativeOne));
                }

                /**  =========== 对账日期 =========== **/
                for (RespReconciliation respReconciliation : lstRecon ){
                    if (respReconciliation.getPlaceOrderId().equals(respPlaceInComeStream.getPlaceOrderId())){
                        respPlaceInComeStream.setReconciliationDate(respReconciliation.getReconciliationDate());
                        break;
                    }
                }
               /* *//**  =========== 消费日期 =========== **//*
                for (RespConsumeTime respConsumeTime : lstConsume ){
                    if (respConsumeTime.getPlaceOrderId().equals(respPlaceInComeStream.getPlaceOrderId())){
                        respPlaceInComeStream.setConsumeTime(respConsumeTime.getConsumeTime());
                        break;
                    }
                }*/

                /**  =========== 支付方式 =========== **/
                for(PayItem payItem : lstPayItem){
                    if(payItem.getPlaceOrderId().equals(respPlaceInComeStream.getPlaceOrderId())){
                        if (payItem.getPayType().intValue() == 7 || payItem.getPayType().intValue() == 10 || payItem.getPayType().intValue() == 11){ // 优惠方式
                            String tempPre = respPlaceInComeStream.getPreferentialType() + payItem.getPayType() + "/";
                            respPlaceInComeStream.setPreferentialType(tempPre);
                            if(payItem.getPayType().intValue() == 7)//鸟蛋金额
                                respPlaceInComeStream.setBirdEggAmount(payItem.getBirdEggAmount());
                            else if(payItem.getPayType().intValue() == 11) //优惠券
                                respPlaceInComeStream.setCouponAmount(payItem.getAmount());
                            else if (payItem.getPayType().intValue() == member_pay_type){//会员卡
                                respPlaceInComeStream.setMemberPayType(member_pay_type);
                            }
                        }else if (payItem.getPayType().intValue() == CG_BOSS_SETTLE || payItem.getPayType().intValue() == WB_BOSS_SETTLE || payItem.getPayType().intValue() == ZB_BOSS_SETTLE){//三种结算方式
                                respPlaceInComeStream.setThreeAmount(payItem.getAmount());
                                respPlaceInComeStream.setActualPayAmount(respPlaceInComeStream.getActualPayAmount().add(payItem.getAmount()));  //实际支付
                        }else {
                            respPlaceInComeStream.setActualPayAmount(respPlaceInComeStream.getActualPayAmount().add(payItem.getAmount()));  //实际支付
                        }
                        String payType = respPlaceInComeStream.getPayType() + payItem.getPayType() + "/"; //支付方式
                        respPlaceInComeStream.setPayType(payType); //支付方式
                    }
                }

                /**  =========== 会员卡消费 =========== **/
                if(lstMemberResult != null && lstMemberResult.size() > 0){
                    for (RespPlaceInComeStream ms : lstMemberResult){
                        if (ms.getPlaceOrderId().equals(respPlaceInComeStream.getPlaceOrderId())){
                            respPlaceInComeStream.setMemberAmount(ms.getMemberAmount());
                            respPlaceInComeStream.setPreferentialAmount(ms.getPreferentialAmount());
                            respPlaceInComeStream.setCardAmount(ms.getCardAmount());
                            respPlaceInComeStream.setCardNumber(ms.getCardNumber());
                            respPlaceInComeStream.setOpCardCount(ms.getOpCardCount());
                            respPlaceInComeStream.setCardType(ms.getCardType());
                            break;
                        }
                    }
                }

                /**  =========== 购买方式 =========== **/
                for(BuyType buyType : lstBuyType){
                    if(respPlaceInComeStream.getPlaceId().intValue() == buyType.getPlaceId().intValue()){
                        String tmpBuyType = respPlaceInComeStream.getBuyType() +buyType.getBuyType() +"/";
                        respPlaceInComeStream.setBuyType(tmpBuyType);
                    }
                }

                /**  =========== 收入类型 =========== **/
                if(respPlaceInComeStream.getUserType().intValue() == 0 || respPlaceInComeStream.getUserType().intValue() == 1 || respPlaceInComeStream.getUserType().intValue() == 2  || respPlaceInComeStream.getUserType().intValue() == 3)
                    respPlaceInComeStream.setUserType(1);

                /**  =========== 次卡消费 =========== **/
                if(respPlaceInComeStream.getCardType() != null){
                    if(respPlaceInComeStream.getCardType() == 4){//次卡
                        BigDecimal tm = respPlaceInComeStream.getCardAmount().divide(new BigDecimal(respPlaceInComeStream.getCardNumber()),10, RoundingMode.HALF_DOWN);
                        if(respPlaceInComeStream.getOpCardCount() != null){
                            tm = tm.multiply(new BigDecimal(respPlaceInComeStream.getOpCardCount()));
                        }
                        respPlaceInComeStream.setMemberAmount(tm);
                    }
                }



                /**  =========== 场地销售金额 =========== **/
                BigDecimal debit;  // 总金额 - 鸟蛋金额 -  优惠券金额 - 会员金额 - 内部结算
                if(respPlaceInComeStream.getOrderStatus().equals("5")){//退款
                    /*******************实付金额**************************/
                    respPlaceInComeStream.setDebit(respPlaceInComeStream.getActualPayAmount().multiply(negativeOne));
                    /*******************优惠总金额***************************/
                    respPlaceInComeStream.setFavourableAmount(respPlaceInComeStream.getOriginalAmount().subtract(respPlaceInComeStream.getActualPayAmount()).subtract(respPlaceInComeStream.getMemberAmount()));
                    respPlaceInComeStream.setMemberAmount(respPlaceInComeStream.getMemberAmount().multiply(negativeOne));
                }
                else{
                    /*******************实付金额**************************/
                    respPlaceInComeStream.setDebit(respPlaceInComeStream.getActualPayAmount());
                    /*******************优惠总金额***************************/
                    respPlaceInComeStream.setFavourableAmount(respPlaceInComeStream.getOriginalAmount().subtract(respPlaceInComeStream.getActualPayAmount()).subtract(respPlaceInComeStream.getMemberAmount()));
                }


                if (respPlaceInComeStream.getUserType() == 4 && respPlaceInComeStream.getMemberPayType().intValue() == 10)// 多种经营 且 会员卡支付
                    respPlaceInComeStream.setDebit(zero); //多种经营金额 = 会员卡金额

                if( respPlaceInComeStream.getMemberPayType().intValue() != 10)
                    respPlaceInComeStream.setMemberAmount(zero);

                if (respPlaceInComeStream.getFavourableAmount().compareTo(zero) != 0 ){
                    if( respPlaceInComeStream.getHandAdjustAmount() != null && respPlaceInComeStream.getHandAdjustAmount().compareTo(zero) != 0){
                        String tmp = respPlaceInComeStream.getPreferentialType() + 35  +"/";
                        respPlaceInComeStream.setPreferentialType(tmp);
                    }
                }
            }
        }
        if(page.getCurrentPage() > page.getTotalPage()){
            lstResult = new ArrayList<>();
            page.setTotalPage(0);
            page.setTotalResult(0);
        }

        logger.info("结束查询场地收入流水---》{}",lstResult.toString());
        PageObj<List<RespPlaceInComeStream>> result = PageObj.create(page.getTotalResult(), page.getCurrentPage(), page.getShowCount(), lstResult);
        return APIResponse.returnSuccess(result);
    }
}
